<?php

namespace maotool\view\html;

use DOMDocument;
use maotool\util\HtmlUtil;

/**
 * @property string $outerHTML
 * @property string $id
 * @property string $innerHTML
 * @property string $innerText
 * @property array $children
 * @property array $childNodes
 * @property Element $body
 * @property Element $head
 */
class Document
{
    /**
     * @var DOMDocument
     */
    private $document;

    public function loadHtml($content)
    {
        $this->document = new DOMDocument('1.0', 'UTF-8');
        $this->document->formatOutput = true;
        $this->document->preserveWhiteSpace = true;
        @$this->document->loadHTML($content, LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL);
    }

    public function loadComponent($content)
    {
        $this->document = new DOMDocument('1.0', 'UTF-8');
        $this->document->formatOutput = true;
        $this->document->preserveWhiteSpace = true;
        @$this->document->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL);
    }

    public function __get($name)
    {
        switch ($name) {
            case 'tagName':
                return "#DOCUMENT";
            case 'innerHTML':
            case 'outerHTML':
                $html = "";
                foreach ($this->document->childNodes as $childNode) {
                    if ($childNode->nodeName == "#text") {
                        $html = $html . $this->document->saveHtml($childNode);
                    } else {
                        if (HtmlUtil::isDoctype($this->document->saveXml($childNode))) {
                            $html = $html . $this->document->saveXml($childNode);
                        } else {
                            $html = $html . $this->document->saveHtml($childNode);
                        }
                    }
                }
                return $html;
            case 'children':
                $children = [];
                foreach ($this->document->childNodes as $childNode) {
                    if ($childNode->nodeName != "#text") {
                        $children[] = new Element($childNode);
                    }
                }
                return $children;
            case 'childNodes':
                $childNodes = [];
                foreach ($this->document->childNodes as $childNode) {
                    if ($childNode->nodeName == "#text") {
                        $childNodes[] = new Text($childNode);
                    } else {
                        $childNodes[] = new Element($childNode);
                    }
                }
                return $childNodes;
            case "head":
                $childNodes = $this->document->getElementsByTagName("head");
                foreach ($childNodes as $childNode) {
                    return new Element($childNode);
                }
                return null;
            case "body":
                $childNodes = $this->document->getElementsByTagName("body");
                foreach ($childNodes as $childNode) {
                    return new Element($childNode);
                }
                return null;
        }
        return $this->$name;
    }

    public function __set($name, $value)
    {
        switch ($name) {
            case 'innerHTML':
            case 'outerHTML':
                $this->document = new DOMDocument('1.0', 'UTF-8');
                $this->document->formatOutput = true;
                $this->document->preserveWhiteSpace = true;
                @$this->document->loadHTML($value, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL);
                break;
            case 'innerText':
                while ($this->document->hasChildNodes()) {
                    $this->document->childNodes[0]->parentNode->removeChild($this->document->childNodes[0]);
                }
                $this->document->appendChild($this->document->createTextNode($value));
                break;
            default:
                $this->$name = $value;
                break;
        }
    }

}